package com.xiaohu.mayi_huiyuan.common;

import com.xiaohu.mayi_huiyuan.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;


@Component
public class Mail {

    private final static Logger logger = LoggerFactory.getLogger(Exception.class);


    @Value("${mysql.backup.file.path}")
    private String filePath;


    @Bean
    public JavaMailSender getMailSender(){
        JavaMailSenderImpl javaMailSender=new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setPassword("ltwupafgivdpbjed");
        javaMailSender.setUsername("296564447@qq.com");
        javaMailSender.setPort(587);
        Properties properties=new Properties();
        properties.setProperty("mail.smtp.auth","true");
        properties.setProperty("mail.smtp.starttls.enable","true");
        properties.setProperty("mail.smtp.starttls.required","true");
        javaMailSender.setProtocol("smtp");
        javaMailSender.setJavaMailProperties(properties);
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(javaMailSender.getUsername(), javaMailSender.getPassword());
           }
       };
        javaMailSender.setSession(Session.getInstance(properties, authenticator));;
        return  javaMailSender;
    }


    @Value("${mail.fromMail.addr}")
    private String form;

    /**
     * 发送简单邮件
     *
     * @param to      接受者
     * @param subject 主题
     * @param content 内容
     */

    public void sendMail (String to, String subject, String content,String fileName)throws Exception {
        MimeMessage mailMessage = getMailSender().createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mailMessage,true);
        helper.setFrom(form);//发起者
        helper.setTo(to);//接受者
        //如果发给多个人的：
        //mailMessage.setTo("1xx.com","2xx.com","3xx.com");    
        helper.setSubject(subject);
        helper.setText(content);
        FileSystemResource file=new FileSystemResource(new File(filePath+"/"+fileName));
        logger.info("！"+ filePath+"/"+fileName);
        helper.addAttachment(fileName,file);
        try {
            getMailSender().send(mailMessage);
            logger.info("发送邮件成功！"+ DateUtil.formatDate(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("发送邮件异常！"+ DateUtil.formatDate(new Date())+"|||"+e.getMessage());
        }


    }

}
